package com.baomidou.springboot.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springboot.entity.User;
import com.baomidou.springboot.mapper.UserMapper;
import com.baomidou.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户服务实现类
 * </p>
 *
 * @author QQQ
 * @since 2018-06-19
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void save(User user) {
        userMapper.insert(user);
    }

    @Override
    public void delete(String id) {
        userMapper.deleteById(id);
    }

    @Override
    public User edit(String id) {
        return userMapper.selectById(id);
    }

    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    @Override
    public User view(String id) {
        return userMapper.selectById(id);
    }

    @Override
    public Page<User> list(Page<User> page, String search) {
        if (0 == page.getSize()) {
            page.setSize(10);
        }
        if (0 == page.getCurrent()) {
            page.setCurrent(1);
        }
        List<User> userList = userMapper.selectList(null);
        page.setRecords(userList);
        return page;
    }

    @Override
    public void deleteBatch(List<String> ids) {
        if (null == ids) {
            return;
        }
        userMapper.deleteBatchIds(ids);
    }

    @Override
    public User findByUsernameAndPassword(String username, String password) {
        return userMapper.findByUsernameAndPassword(username, password);
    }
}
